package com.zhonghui.controller;

import com.zhonghui.common.core.result.FwResult;
import com.zhonghui.dto.*;
import com.zhonghui.model.User;
import com.zhonghui.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author: FL
 * @create: 2024 2024/5/16 23:32
 */
@RestController
@Api(value = "用户接口")
@Slf4j
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("密码登录")
    @PostMapping("/login/password")
    public FwResult<Map<String,Object>> login(@RequestBody @Valid PhoneLoginDto phoneLoginDto){
        log.info("用户登录{}", phoneLoginDto.getPhone());
        return userService.passwordLogin(phoneLoginDto);
    }

    @ApiOperation("邮箱登录")
    @PostMapping("/login/email")
    public FwResult<Map<String,Object>> login(@RequestBody @Valid VerifyEmailDto emailDto){
        log.info("用户登录{}", emailDto.getEmail());
        return userService.emailLogin(emailDto);
    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public FwResult<User> logout(@RequestHeader(value = "Authorization",required = false) String token){
        log.info("用户登出{}",token);
        return userService.logout(token);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public FwResult registerMail(@RequestBody UserMailRegisterDto user) {
        return userService.register(user);
    }

    @ApiOperation("获取验证码")
    @PostMapping("/email/getCode")
    public FwResult getCode(@RequestParam("email") String email) {
        userService.generateCode(email,"user");
        return FwResult.okMsg("获取验证码成功");
    }

    @ApiOperation("验证邮箱")
    @PostMapping("/email/verify")
    public FwResult verifyEmail(@RequestBody VerifyEmailDto dto) {
        log.info("用户验证邮箱{}",dto.getCode());
        return userService.verifyMail(dto);
    }

    @ApiOperation("重置密码")
    @PostMapping("/email/resetPassword")
    public FwResult resetPassword(@RequestBody ResetPasswordDto dto) {
        log.info("重置用户密码");
        return userService.resetPassword(dto);
    }

    @ApiOperation("启用用户")
    @PostMapping("/user/enable")
    public FwResult enableUser(@RequestBody EnableUserDto dto) {
        log.info("启用用户{}",dto.getEmail());
        return userService.enableUser(dto);
    }

    @PostMapping("/user/list")
    @ApiOperation("获取用户列表")
    public FwResult getUserList(@RequestBody PageDto pageDto) {
        return userService.getUserList(pageDto);
    }


}
